package com.mkh.st.admin.strategy;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mkh.st.admin.api.entity.SysBusinessEntity;
import com.mkh.st.admin.api.entity.SysCompanyInfo;
import com.mkh.st.admin.api.entity.SysUserRoleScope;
import com.mkh.st.admin.api.vo.DataPermissionVO;
import com.mkh.st.admin.api.vo.ScopeDataDeatil;
import com.mkh.st.admin.api.vo.ScopeDataVO;
import com.mkh.st.admin.api.vo.UserRoleScopeVO;
import com.mkh.st.admin.mapper.SysBusinessEntityMapper;
import com.mkh.st.common.core.constant.CommonConstants;
import com.mkh.st.order.dto.FeignCustomerDTO;
import com.mkh.st.order.feign.ICustomerProvider;
import com.mkh.st.order.vo.FeignCustomerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
public class CustomerStrategy extends BaseScopeStrategy{

	private final List<Long> ids= Arrays.asList(-1L);

	@Autowired
	private ICustomerProvider customerProvider;

	@Autowired
	private SysBusinessEntityMapper businessEntityMapper;

	private final Map<Long,String> siteMap=new ConcurrentHashMap<>();

	@Override
	public void setDataPermission(DataPermissionVO vo, SysUserRoleScope sysUserRoleScope, Long tenantId) {
		if(ObjectUtil.isNotNull(sysUserRoleScope)){
			FeignCustomerDTO dto=new FeignCustomerDTO();
			if(CommonConstants.ONE.equals(sysUserRoleScope.getAllFlag())){
				vo.setCustomerAll(Boolean.TRUE);
				dto.setCustomerAll(Boolean.TRUE);
				dto.setTenantId(tenantId);
				List<FeignCustomerVO> list = customerProvider.queryCustomer(dto).getData();
				if(CollectionUtil.isNotEmpty(list)){
					List<Long> customerIds=list.stream().map(FeignCustomerVO::getId).collect(Collectors.toList());
					vo.setCustomerList(customerIds);
				}
			}else {
				if(StrUtil.isNotBlank(sysUserRoleScope.getIds())){
					List<Long> allCuctomerIds= Convert.toList(Long.class, StrUtil.split(sysUserRoleScope.getIds(),','));
					//未删除未禁用查询
					dto.setCustomerAll(Boolean.FALSE);
					dto.setCustomerList(allCuctomerIds);
					dto.setTenantId(tenantId);
					List<FeignCustomerVO> list = customerProvider.queryCustomer(dto).getData();
					if(CollectionUtil.isNotEmpty(list)){
						List<Long> customerIds=list.stream().map(FeignCustomerVO::getId).collect(Collectors.toList());
						vo.setCustomerList(customerIds);
					}else {
						vo.setCustomerList(ids);
					}
				}else {
					vo.setCustomerList(ids);
				}
			}
		}else {
			vo.setCustomerList(ids);
		}
	}

	@Override
	public Long setKey() {
		return 6L;
	}

	@Override
	public ScopeDataDeatil getScopeDataList(DataPermissionVO vo, SysUserRoleScope one,Long scopeId) {
		ScopeDataDeatil deatil=new ScopeDataDeatil();
		deatil.setAllFlag(vo.getCustomerAll());
		deatil.setScopeId(scopeId);
		List<ScopeDataVO> scopeList =new ArrayList<>();
		if(CollectionUtil.isEmpty(vo.getCustomerList())){
			return deatil;
		}
		FeignCustomerDTO dto=new FeignCustomerDTO();
		//当前操作人的客户权限
		dto.setCustomerAll(Boolean.FALSE);
		dto.setCustomerList(vo.getCustomerList());
		List<FeignCustomerVO> list = customerProvider.queryCustomer(dto).getData();
		//被操作账号已有的客户权限
		List<Long> exist=new ArrayList<>();
		boolean flag=false;
		if(ObjectUtil.isNotNull(one)){
			if(one.getAllFlag().equals(CommonConstants.ONE)){
				flag=true;
			}else if(StrUtil.isNotBlank(one.getIds())){
				exist=Convert.toList(Long.class, StrUtil.split(one.getIds(),','));
			}
		}
		if(CollectionUtil.isNotEmpty(list)){
			for (FeignCustomerVO item:list) {
				ScopeDataVO scopeDataVO=new ScopeDataVO();
				scopeDataVO.setId(item.getId()).setCode(item.getSn()).setName(item.getName());
				if(siteMap.containsKey(item.getSiteId())){
					scopeDataVO.setParentName(siteMap.get(item.getSiteId()));
				}else {
					SysBusinessEntity sysBusinessEntity = businessEntityMapper.selectById(item.getSiteId());
					scopeDataVO.setParentName(sysBusinessEntity.getBusinessName());
					siteMap.put(item.getSiteId(),sysBusinessEntity.getBusinessName());
				}
				if(flag || exist.stream().filter(v ->v.equals(item.getId())).findFirst().isPresent()){
					scopeDataVO.setChecked(Boolean.TRUE);
				}
				scopeList.add(scopeDataVO);
			}
			deatil.setScopeList(scopeList);
		}
		return deatil;
	}

	@Override
	public void getUserDataPermission(DataPermissionVO vo, DataPermissionVO item) {
		if(CollectionUtil.isNotEmpty(item.getCustomerList())){
			if(vo.getCustomerAll()){
				return;
			}else if(item.getCustomerAll()){
				vo.setCustomerAll(item.getCustomerAll());
				vo.setCustomerList(item.getCustomerList());
			}else {
				if(CollectionUtil.isNotEmpty(vo.getCustomerList())){
					List<Long> collect = Stream.of(vo.getCustomerList(),item.getCustomerList()).flatMap(Collection::stream).distinct().collect(Collectors.toList());
					vo.setCustomerList(collect);
				}else {
					vo.setCustomerList(item.getCustomerList());
				}
			}
		}
	}

	@Override
	public List<Long> getList(Long tenantId) {
		FeignCustomerDTO dto=new FeignCustomerDTO();
		dto.setCustomerAll(Boolean.TRUE);
		dto.setTenantId(tenantId);
		List<FeignCustomerVO> list = customerProvider.queryCustomer(dto).getData();
		if(CollectionUtil.isNotEmpty(list)){
			List<Long> ids=list.stream().map(FeignCustomerVO::getId).collect(Collectors.toList());
			return	ids;
		}
		return null;
	}

	@Override
	public void getAllList(UserRoleScopeVO userRoleScopeVO, SysUserRoleScope scope, Long tenantId) {
		if(CommonConstants.ZERO.equals(scope.getAllFlag())&& StrUtil.isBlank(scope.getIds())){
			return;
		}
		FeignCustomerDTO dto=new FeignCustomerDTO();
		dto.setTenantId(tenantId);
		if(CommonConstants.ONE.equals(scope.getAllFlag())){
			dto.setCustomerAll(Boolean.TRUE);
		}else {
			dto.setCustomerAll(Boolean.FALSE);
			List<Long> allCuctomerIds= Convert.toList(Long.class, StrUtil.split(scope.getIds(),','));
			dto.setCustomerList(allCuctomerIds);
		}
		List<FeignCustomerVO> list = customerProvider.queryCustomer(dto).getData();
		List<ScopeDataVO> scopeList =new ArrayList<>();
		if(CollectionUtil.isNotEmpty(list)){
			for (FeignCustomerVO item:list) {
				ScopeDataVO scopeDataVO=new ScopeDataVO();
				scopeDataVO.setId(item.getId()).setCode(item.getSn()).setName(item.getName());
				if(siteMap.containsKey(item.getSiteId())){
					scopeDataVO.setParentName(siteMap.get(item.getSiteId()));
				}else {
					SysBusinessEntity sysBusinessEntity = businessEntityMapper.selectById(item.getSiteId());
					scopeDataVO.setParentName(sysBusinessEntity.getBusinessName());
					siteMap.put(item.getSiteId(),sysBusinessEntity.getBusinessName());
				}
				scopeList.add(scopeDataVO);
			}
			userRoleScopeVO.setScopeList(scopeList);
		}
	}

	@Override
	public Boolean checkPermission(SysUserRoleScope one, String name, Long tenantId) {
		Long customerId = customerProvider.getCustomerByName(name,tenantId).getData();
		if(ObjectUtil.isNotNull(customerId)&&StrUtil.isNotBlank(one.getIds())){
			List<Long> ids = Convert.toList(Long.class, StrUtil.split(one.getIds(), ','));
			if(ids.contains(customerId)){
				return Boolean.TRUE;
			}
		}
		return null;
	}
}
